<script lang="ts" setup>
import { ref } from 'vue'
import { GetData } from '@/api/modules/subscriber'
import GetCacheData from '@/store/modules/NewPageCache'

// const imgDomain = 'https://img1.eprhan.cc'
// const router = useRouter()
const cacheData = GetCacheData()
let PageNum = cacheData.GetCacheData('subscriber').currentPage

const findFrom = ref({
  ...(cacheData.GetCacheData('subscriber').fromData),
})
const dataList: any = reactive({
  list: [],
})
const loading = ref(true)
const total = ref(0)

async function GetDataList(PageIndex: number = 1, PageSize: number = 20) {
  const params = {
    PageIndex,
    PageSize,
    ...(findFrom.value.UserId && ({ UserId: findFrom.value.UserId })),
    ...(findFrom.value.SCOpenId && ({ SCOpenId: findFrom.value.SCOpenId })),
    ...(findFrom.value.UserPhone && ({ UserPhone: findFrom.value.UserPhone })),
    ...(findFrom.value.UserNickName && ({ UserNickName: findFrom.value.UserNickName })),
  }
  await GetData(params).then(async (dataRes: any) => {
    if (dataRes.succeeded) {
      dataList.list = dataRes.data.items
      total.value = dataRes.data.total
      loading.value = false
    }
  })
  loading.value = false
}
// 查询
function Inquire() {
  GetDataList(1, cacheData.GetCacheData('subscriber').currentSize)
  cacheData.SetCacheData('subscriber', PageNum, cacheData.GetCacheData('subscriber').currentSize, findFrom.value)
}

// 翻页
function handleSizeChange(val: number) {
  GetDataList(1, val)
  cacheData.SetCacheData('subscriber', PageNum, val)
}
function handleCurrentChange(val: number) {
  GetDataList(val, cacheData.GetCacheData('subscriber').currentSize)
  cacheData.SetCacheData('subscriber', val, cacheData.GetCacheData('subscriber').currentSize)
  PageNum = val
}

const nowPageNum = computed(() => {
  return cacheData.GetCacheData('subscriber').currentPage || 1
})

onMounted(async () => {
  GetDataList(cacheData.GetCacheData('subscriber').currentPage, cacheData.GetCacheData('subscriber').currentSize)
})
</script>

<template>
  <div>
    <PageMain>
      <SearchBar :show-toggle="false">
        <div class="header">
          <el-form label-positio="right" label-width="auto">
            <div class="from">
              <el-form-item label="用户ID">
                <el-input v-model="findFrom.UserId" placeholder="请输入用户ID" clearable @keyup.enter="Inquire" />
              </el-form-item>
              <el-form-item label="OpenId">
                <el-input v-model="findFrom.SCOpenId" placeholder="请输入OpenId" clearable @keyup.enter="Inquire" />
              </el-form-item>
              <el-form-item label="授权手机号">
                <el-input v-model="findFrom.UserPhone" placeholder="请输入授权手机号" clearable @keyup.enter="Inquire" />
              </el-form-item>
              <el-form-item label="微信昵称">
                <el-input v-model="findFrom.UserNickName" placeholder="请输入微信昵称" clearable @keyup.enter="Inquire" />
              </el-form-item>
            </div>
          </el-form>
          <div class="text-right">
            <el-button @click="Inquire">
              查询
            </el-button>
          </div>
        </div>
      </SearchBar>
      <ElDivider border-style="dashed" />
      <div class="mb-5 text-right">
        <!-- <el-button type="success" @click="AddData">
          添加
        </el-button> -->
      </div>
      <el-table v-loading="loading" :data="dataList.list" stripe style="width: 100%;" border>
        <el-table-column align="center" prop="userId" label="用户ID" width="100" />
        <el-table-column align="center" prop="user55NickName" label="顾客信息" width="200">
          <template #default="scope">
            <div v-if="!scope.row.userNickName">
              未授权
            </div>
            <div v-else class="user-name">
              <el-avatar :size="40" style="margin-right: 10px;" :src="scope.row.userAvatar" />
              <div>
                <p style="margin: 0;">
                  {{ scope.row.userNickName }}
                </p>
                <span>
                  {{ scope.row.userPhone }}
                </span>
              </div>
            </div>
          </template>
        </el-table-column>
        <!-- <el-table-column align="center" prop="gender" label="性别" width="160">
          <template #default="scope">
            <el-tag>
              {{ scope.row.gender === 1 ? '男'
                : scope.row.gender === 2 ? '女'
                  : scope.row.gender === 0 ? '未知' : '未定义' }}
            </el-tag>
          </template>
        </el-table-column> -->
        <el-table-column align="center" prop="scOpenId" label="OpenId" width="300" />
        <el-table-column align="center" prop="unionId" label="UnionId" width="300" />
        <el-table-column header-align="center" prop="desp" label="备注" width="300" />
        <el-table-column align="center" prop="firstQwTime" label="加企微时间" width="230">
          <template #default="scope">
            <span>
              {{ scope.row.firstQwTime ? scope.row.firstQwTime : '未加' }}
            </span>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="addTime" label="加入时间" width="230" />
        <el-table-column align="center" prop="updateTime" label="更新时间" width="230" />
        <el-table-column align="center" prop="gender" label="授权时间" width="230">
          <template #default="scope">
            <span>
              {{ scope.row.userPhone ? scope.row.bindTime : '未授权' }}
            </span>
          </template>
        </el-table-column>
        <!-- <el-table-column align="center" label="操作" fixed="right" min-width="400">
          <template #default="scope">
            <el-button type="primary" @click="ViewDetails(scope.row.subscriberInfo?.userId, scope.row.drawId)">
              查看机会明细
            </el-button>
            <el-button type="success" @click="OpenView(scope.row.subscriberInfo?.userId, scope.row.drawId)">
              发送抽奖机会
            </el-button>
            <el-button type="danger" @click="Delete(scope.row.subscriberInfo?.id)">
              作废抽奖机会
            </el-button>
          </template>
        </el-table-column> -->
      </el-table>
      <div style="height: 10px;" />
      <el-pagination
        :default-page-size="cacheData.GetCacheData('subscriber').currentSize"
        :page-sizes="[5, 10, 20, 50, 100]"
        layout="total, sizes, ->, prev, pager, next, jumper"
        :total="total"
        :default-current-page="nowPageNum"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </PageMain>
  </div>
</template>

<style scoped>
.header {
  display: grid;
  grid-template-columns: 6fr 1fr;

  .from {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
    grid-gap: 20px;

    .el-form-item {
      margin-bottom: 0;
    }
  }
}

.user-name {
  display: flex;
  align-items: center;
  justify-content: left;

  div {
    text-align: left;
  }
}
</style>
